﻿using System.Diagnostics;
using HLogger = HK.Core.Logs.Logger;
using HLogLevel = HK.Core.Logs.Protocol.LogLevel;

namespace HK.Core.Common
{
    /// <summary>
    /// 类扩展
    /// </summary>
    public class ClassExtension
    {
        /// <summary>
        /// 类名.
        /// </summary>
        private string _className;
        protected string ClassName {
            get { 
                if(false == string.IsNullOrEmpty(_className)) {
                    return _className;
                }
                _className = GetType().Name;
                return _className;
            }
        }
        
        

        /// <summary>
        /// 信息日志(默认：运行日志).
        /// </summary>
        /// <param name="iFormat">格式.</param>
        /// <param name="iArgs">参数.</param>
        [Conditional("HK_LOG_INFO")]
        protected void Info(string iFormat, params object[] iArgs) {
            if (HLogger.LogLevel > HLogLevel.RInfo) {
                return;
            }
            var log = string.Format(iFormat, iArgs);
            HLogger.Info ($"[{ClassName}] {log}");
        }

        /// <summary>
        /// 警告日志.
        /// </summary>
        /// <param name="iFormat">格式.</param>
        /// <param name="iArgs">参数.</param>
        [Conditional("HK_LOG_WARNING")]
        protected void Warning(string iFormat, params object[] iArgs) {
            if (HLogger.LogLevel > HLogLevel.Warning) {
                return;
            }
            var log = string.Format(iFormat, iArgs);
            HLogger.Warning ($"[{ClassName}] {log}");
        }

        /// <summary>
        /// 信息:逻辑(LI).
        /// </summary>
        /// <param name="iFormat">格式.</param>
        /// <param name="iArgs">参数.</param>
        protected void LInfo(string iFormat, params object[] iArgs) {
            if (HLogger.LogLevel > HLogLevel.LInfo) {
                return;
            }
            var log = string.Format(iFormat, iArgs);
            HLogger.LInfo ($"[{ClassName}] {log}");
        }

        /// <summary>
        /// 错误日志.
        /// </summary>
        /// <param name="iFormat">格式.</param>
        /// <param name="iArgs">参数.</param>
        protected void Error(string iFormat, params object[] iArgs) {
            if (HLogger.LogLevel > HLogLevel.Error) {
                return;
            }
            var log = string.Format(iFormat, iArgs);
            HLogger.Error ($"[{ClassName}] {log}");
        }

        /// <summary>
        /// 致命日志.
        /// </summary>
        /// <param name="iFormat">格式.</param>
        /// <param name="iArgs">参数.</param>
        protected void Fatal(string iFormat, params object[] iArgs) {
            if (HLogger.LogLevel > HLogLevel.Fatal) {
                return;
            }
            var log = string.Format(iFormat, iArgs);
            HLogger.Fatal ($"[{ClassName}] {log}");
        }
    }
}
